﻿namespace PrismEx.SampleApp.Modules.ParameterizedNavigation
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.Composition;
    using System.Windows.Input;

    using Microsoft.Expression.Interactivity.Core;
    using Microsoft.Practices.Prism.Events;

    using PrismEx.Base;
    using PrismEx.Events;
    using PrismEx.Events.Navigation;
    using PrismEx.Extensions;
    using PrismEx.SampleApp.Model;

    [Export]
    public class ListViewModel : Observable
    {
        private readonly CustomerRepository repository;

        private readonly IEventAggregator eventAggregator;

        [ImportingConstructor]
        public ListViewModel(CustomerRepository repository, IEventAggregator eventAggregator)
        {
            this.repository = repository;
            this.eventAggregator = eventAggregator;

            ShowCustomerDetails = new ActionCommand(
                o =>
                    { 
                        var customer = o as Customer;
                        var uri = NavigationUriBuilder.ForType<DetailView>().WithParameter("CustomerId", customer.Id.ToString());

                        eventAggregator.Publish(
                            new NavigateUriEvent
                                {
                                    RegionName = "CustomerDetailsRegion", 
                                    Uri = uri
                                });
                    });
        }

        public IEnumerable<Customer> Customers
        {
            get
            {
                return repository.GetCustomers();
            }
        }

        public ICommand ShowCustomerDetails { get; private set; }
    }
}